package com.boot2.core.executor;

import com.boot2.core.utils.BeanUtils;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.util.concurrent.ListenableFuture;

import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadPoolExecutor;

/**
* @author zhangweilin
* @date 2018/6/12 15:36
* @description:  线程池工具
*/
@Slf4j
public class VisiableThreadPoolTaskExecutor extends ThreadPoolTaskExecutor {
//    protected static final Logger logger = LoggerFactory.getLogger(VisiableThreadPoolTaskExecutor.class);

    private void showThreadPoolInfo(String prefix){
        ThreadPoolExecutor threadPoolExecutor = getThreadPoolExecutor();
        if(null==threadPoolExecutor){
            return ;
        }
        if (log.isDebugEnabled()) {
            log.debug("{}, {},taskCount [{}], completedTaskCount [{}], activeCount [{}], poolSize [{}], corePoolSize [{}],largestPoolSize [{}],maximumPoolSize [{}], queueSize [{}]",
                    this.getThreadNamePrefix(),
                    prefix,
                    threadPoolExecutor.getTaskCount(),
                    threadPoolExecutor.getCompletedTaskCount(),
                    threadPoolExecutor.getActiveCount(),
                    threadPoolExecutor.getPoolSize(),
                    threadPoolExecutor.getCorePoolSize(),
                    threadPoolExecutor.getLargestPoolSize(),
                    threadPoolExecutor.getMaximumPoolSize(),
                    threadPoolExecutor.getQueue().size());
        }
    }

    public ThreadPoolExecutorVo getThreadPoolExecutorVo() {
        ThreadPoolExecutor threadPoolExecutor = getThreadPoolExecutor();
        if(null==threadPoolExecutor){
            return null;
        }
        ThreadPoolExecutorVo threadPoolExecutorVo = new ThreadPoolExecutorVo();
        BeanUtils.copyProperties(threadPoolExecutor,threadPoolExecutorVo);
        return threadPoolExecutorVo;
    }

    @Data
    static class ThreadPoolExecutorVo {
        private long taskCount;
        private long completedTaskCount;
        private long activeCount;
        private long poolSize;
        private long corePoolSize;
        private long largestPoolSize;
        private long maximumPoolSize;
        private long queueSize;
    }


    @Override
    public void execute(Runnable task) {
        showThreadPoolInfo("1. do execute: ");
        super.execute(task);
    }

    @Override
    public void execute(Runnable task, long startTimeout) {
        showThreadPoolInfo("2. do execute: ");
        super.execute(task, startTimeout);
    }

    @Override
    public Future<?> submit(Runnable task) {
        showThreadPoolInfo("1. do submit: ");
        return super.submit(task);
    }

    @Override
    public <T> Future<T> submit(Callable<T> task) {
        showThreadPoolInfo("2. do submit: ");
        return super.submit(task);
    }

    @Override
    public ListenableFuture<?> submitListenable(Runnable task) {
        showThreadPoolInfo("1. do submitListenable: ");
        return super.submitListenable(task);
    }

    @Override
    public <T> ListenableFuture<T> submitListenable(Callable<T> task) {
        showThreadPoolInfo("2. do submitListenable: ");
        return super.submitListenable(task);
    }


}